package com.sailing.lianxi.thread;

import java.util.Date;

import com.sailing.lianxi.util.datetime.DateFormatTool;

/**
 * 创建线程方式3-线程调用同一份实例
 * 线程访问同一个东西(同一个类，方法、变量)就会引起线程安全问题，
 * @author Administrator
 *
 */
public class MyRunnableSyn implements Runnable {
	private  int tickets =100;
	public void run() {
	    System.out.println("线程："+Thread.currentThread().getName()+" 开始执行:"+DateFormatTool.format(new Date(), DateFormatTool.Y_M_D_HMS));
	    try {
            Thread.sleep(3000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            
        }
		while(true){
				//if如果不加锁就会出现一个
				if(tickets>0){
	//				System.out.println(Thread.currentThread().getName()+",正在出售第"+(tickets--)+"张票");
					System.out.println(Thread.currentThread().getName()+",value="+(tickets--));
				
				}else{
					break;
				}
		}
//		System.out.println("线程："+Thread.currentThread().getName()+" 执行结束");
	}
	
	public static void main(String[] args) {
		MyRunnableSyn myRunnable2 = new MyRunnableSyn();
		 for(int i=0;i<5;i++){
			 Thread t1 = new Thread(myRunnable2);
			 t1.start();
		 }
	}
}
